﻿using System;
using System.Collections.Generic;
using System.Linq;

class Application
{
    const int AssasinationDistance = 20;
    const int BlindShotDistance = 38;
    const int BulletRadarRange = 35;
    const int RunInCirclesDistance = 10;

    static void Main(string[] args)
    {
        //Console.SetIn(System.IO.File.OpenText(@"..\..\input1.txt"));
        int radius = int.Parse(Console.ReadLine());
        string playerInfo = Console.ReadLine();
        BattleFrog player = CreateFrog(playerInfo);
        string enemyInfo = Console.ReadLine();
        var RandomGenerator = new Random();

        BattleFrog enemy = CreateFrog(enemyInfo);
        int bulletCount = int.Parse(Console.ReadLine());
        List<Bullet> bullets = new List<Bullet>();
        for (int i = 0; i < bulletCount; i++)
        {
            string bulletInfo = Console.ReadLine();
            bullets.Add(CreateBullet(bulletInfo));
        }

        bullets = bullets.FindAll(x => CalculateDistance(x.DestinationX, x.DestinationY, player.PossitionX, player.PossitionY) < BulletRadarRange);

        double distanceToEnemy = CalculateDistance(player.PossitionX, player.PossitionY, enemy.PossitionX, enemy.PossitionY);

        if ((distanceToEnemy < AssasinationDistance && player.Cooldown != 0) ||
            (distanceToEnemy < AssasinationDistance && enemy.Cooldown == 0))
        {
            player.MoveMode = PlayerState.Runaway;
        }

        for (int i = 0; i < bullets.Count; i++)
        {
            //Estimating distance between bullets 2 points and player's possition
            var distanceBetweenCurrentAndPlayer = CalculateDistance(player.PossitionX, player.PossitionY, bullets[i].PossitionX, bullets[i].PossitionY);
            var distanceBetweenDestinationAndPlayer = CalculateDistance(player.PossitionX, player.PossitionY, bullets[i].DestinationX, bullets[i].DestinationY);

            if (distanceBetweenCurrentAndPlayer > distanceBetweenDestinationAndPlayer)
            {
                // the bullet is flying to us, so we enter SurvivalMode
                //implement SurvivolMode 
                player.EnterSurvivalMode(bullets[i]);
            }
        }

        if (player.MoveMode == PlayerState.Attack)
        {
            player.Move(enemy.PossitionX, enemy.PossitionY);
        }

        else if (player.MoveMode == PlayerState.Dodge)
        {
            player.Move(player.DestinationX, player.DestinationY);
        }

        else if (player.MoveMode == PlayerState.Runaway)
        {
            if ((player.PossitionX == 0 && player.PossitionY == 0) || (Math.Abs(player.PossitionX) < RunInCirclesDistance && Math.Abs(player.PossitionY) < RunInCirclesDistance))
            {
                var xDeviation = RandomGenerator.Next(5, 10);
                var yDeviation = RandomGenerator.Next(5, 10);

                player.Move(enemy.PossitionX + xDeviation, enemy.PossitionY + yDeviation);

            }
            else
            {
                player.Move(0, 0);
            }
        }

        if (distanceToEnemy > BlindShotDistance)
        {
            if (player.Cooldown == 0)
            {
                player.Shoot(enemy.PossitionX, enemy.PossitionY);
            }
            else
            {
                Console.WriteLine();
            }
        }
        else
        {

            if (distanceToEnemy < AssasinationDistance && player.Cooldown == 0)
            {
                player.Shoot(enemy.PossitionX, enemy.PossitionY);
            }
            else
            {
                Console.WriteLine();
            }
        }

        //Plan: 
        //1.1  Calculate all possible hits of the bullets
        //1.2 Check if any bullet is targeting us
        //1.3 Enter Survivol Mode if neccessary; 
        //2. Check for enemy possition
        //3. Check where is the end of the Gamefield and act if needed
        //4. Check if we must shoot
        //5. Check where to move 
        //6. Print output
    }

    private static double CalculateDistance(double firstX, double firstY, double secondX, double secondY)
    {
        var result = Math.Sqrt((firstX - secondX) * (firstX - secondX) + (firstY - secondY) * (firstY - secondY));
        return result;
    }

    private static BattleFrog CreateFrog(string info)
    {
        string[] tokens = info.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        double positionX = double.Parse(tokens[0]);
        double positionY = double.Parse(tokens[1]);
        int coolDown = int.Parse(tokens[2]);

        return new BattleFrog(positionX, positionY, coolDown);
    }

    private static Bullet CreateBullet(string info)
    {
        string[] tokens = info.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        double currentPositionX = double.Parse(tokens[0]);
        double currentPositionY = double.Parse(tokens[1]);
        double destinationPositionX = double.Parse(tokens[2]);
        double destinationPositionY = double.Parse(tokens[3]);

        return new Bullet(currentPositionX, currentPositionY, destinationPositionX, destinationPositionY);

    }
}

